package model

import (
	"database/sql"

	"github.com/go-xorm/xorm"
)

// Reader represents the t_reader table.
type Reader struct {
	ID       int            `xorm:"'id' pk autoincr"`
	BM       string         `xorm:"'bm'"`
	Name     string         `xorm:"'name'"`
	SFZH     string         `xorm:"'sfzh'"`
	Sex      sql.NullString `xorm:"'sex' null"`
	CSRQ     sql.NullString `xorm:"'csrq' null"`
	Pass     string         `xorm:"'pass'"`
	Oherbh   sql.NullString `xorm:"'oherbh' null"`
	School   sql.NullString `xorm:"'school' null"`
	Whcd     sql.NullString `xorm:"'whcd' null"`
	Duty     sql.NullString `xorm:"'duty' null"`
	ZC       sql.NullString `xorm:"'zc' null"`
	YB       sql.NullString `xorm:"'yb' null"`
	Phone    sql.NullString `xorm:"'phone' null"`
	Unit     sql.NullString `xorm:"'unit' null"`
	LType    string         `xorm:"'ltype'"`
	Address  sql.NullString `xorm:"'address' null"`
	Emial    sql.NullString `xorm:"'Emial' null"` // Note: Corrected typo in column name
	Remark   sql.NullString `xorm:"'remark' null"`
	CDRQ     string         `xorm:"'cdrq'"`
	Statues  string         `xorm:"'statues'"` // Note: Corrected typo in column name
	Startdt  string         `xorm:"'startdt'"`
	Enddt    string         `xorm:"'enddt'"`
	Ljnumb   sql.NullInt64  `xorm:"'Ljnumb' null"`
	Lastjydt sql.NullString `xorm:"'lastjydt' null"`
	Lastdgdt sql.NullString `xorm:"'lastdgdt' null"`
	Khgdm    sql.NullString `xorm:"'khgdm' null"`
	YJ       sql.NullString `xorm:"'yj' null"`
	Ptwjs    sql.NullInt64  `xorm:"'ptwjs' null"`
	Xyjs     int            `xorm:"'xyjs'"`
	Ptwjqk   string         `xorm:"'ptwjqk'"`
	Admin    string         `xorm:"'admin'"`
	Adminrq  string         `xorm:"'adminrq'"`
}

// TableName returns the name of the table in the database.
func (r Reader) TableName() string {
	return "t_reader"
}

// CreateReader creates a new record in the database.
func CreateReader(engine *xorm.Engine, record *Reader) error {
	_, err := engine.Insert(record)
	return err
}

// GetReader retrieves a record by bm from the database.
func GetReaderByBm(engine *xorm.Engine, bm string) (*Reader, error) {
	record := new(Reader)
	has, err := engine.Where("bm = ?", bm).Get(record)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, xorm.ErrNotExist
	}
	return record, nil
}

func GetReaderBySfzh(engine *xorm.Engine, sfzh string) (*Reader, error) {
	record := new(Reader)
	has, err := engine.Where("sfzh = ?", sfzh).Get(record)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, xorm.ErrNotExist
	}
	return record, nil
}

func GetReaderByID(engine *xorm.Engine, id int) (*Reader, error) {
	record := new(Reader)
	has, err := engine.Where("id = ?", id).Get(record)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, xorm.ErrNotExist
	}
	return record, nil
}

// UpdateReader updates a record in the database.
func UpdateReader(engine *xorm.Engine, newRecord *Reader) error {
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	_, err := session.ID(newRecord.ID).Update(newRecord)
	if err != nil {
		if rErr := session.Rollback(); rErr != nil {
			return rErr
		}
		return err
	}
	if err := session.Commit(); err != nil {
		return err
	}
	return nil
}
